<?php
$title = '策略市场';
/** @var array $strategies */
/** @var array $subscriptionMap */
require __DIR__ . '/../partials/head.php';
require __DIR__ . '/../partials/nav.php';
require __DIR__ . '/../partials/toast.php';
?>
<section class="section">
    <div class="app-container">
        <h2 style="margin-bottom:24px;">策略市场</h2>
        <?php if (!empty($_GET['saved'])): ?>
            <div class="card" style="background:rgba(10,132,255,0.08);border:1px solid rgba(10,132,255,0.2);color:#0a84ff;">订阅设置已保存</div>
        <?php endif; ?>
        <?php if (empty($strategies)): ?>
            <div class="empty-state">
                <img src="/assets/img/empty/strategies.svg" alt="空状态插画">
                <p>暂无策略可用，请稍后再来或联系管理员上架。</p>
            </div>
        <?php else: ?>
            <div class="grid grid-3">
                <?php foreach ($strategies as $script): ?>
                    <?php $sub = $subscriptionMap[$script['code']] ?? null; ?>
                    <div class="card" style="gap:18px;">
                        <div>
                            <h3><?= htmlspecialchars($script['name']) ?></h3>
                            <p style="color:var(--text-secondary);margin:4px 0;">Connector：<?= htmlspecialchars($script['connector']) ?> · 标的：<?= htmlspecialchars(implode(', ', $script['symbols'])) ?></p>
                            <span class="badge">状态：<?= htmlspecialchars($script['status']) ?></span>
                        </div>
                        <form method="post" action="/strategies/subscribe" style="display:flex;flex-direction:column;gap:12px;">
                            <input type="hidden" name="script_code" value="<?= htmlspecialchars($script['code']) ?>">
                            <div class="field">
                                <label>每次投入额度（USDT）</label>
                                <input type="number" name="allocation" min="10" step="10" value="<?= htmlspecialchars((string)($sub['allocation']['value'] ?? $script['default_target']['value'] ?? 100)) ?>" required>
                                <small style="color:var(--text-secondary);">请勿低于交易所最小名义额，建议 ≥ 50 USDT。</small>
                            </div>
                            <label class="switch" style="align-self:flex-start;">
                                <input type="checkbox" name="enabled" <?= ($sub['enabled'] ?? false) ? 'checked' : '' ?>>
                                <span class="slider"></span>
                            </label>
                            <div style="display:flex;gap:10px;">
                                <button class="btn" type="submit">保存订阅</button>
                                <a class="btn-ghost btn" href="/strategies/<?= urlencode($script['code']) ?>">查看详情</a>
                            </div>
                        </form>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</section>
<?php require __DIR__ . '/../partials/footer.php'; ?>
